package org.wolfengi.monitor.service.impl;

import org.wolfengi.monitor.common.ShipCN;
import org.wolfengi.monitor.entity.FShipTravelRateCount;
import org.wolfengi.monitor.mapper.FShipTravelRateCountMapper;
import org.wolfengi.monitor.service.FShipTravelRateCountService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wolfengi
 * @since 2020-05-22
 */
@Service
public class FShipTravelRateCountServiceImpl extends ServiceImpl<FShipTravelRateCountMapper, FShipTravelRateCount> implements FShipTravelRateCountService {

    @Autowired
    private FShipTravelRateCountMapper fShipTravelRateCountMapper;

    @Override
    public Map<String, Object> shipTravelRateCount() {
        List<String> dates = fShipTravelRateCountMapper.selectDateForShipTravelRate();
        List<String> ships = fShipTravelRateCountMapper.selectShipTypeForShipTravelRate();
        List<Map> datas = fShipTravelRateCountMapper.selectShipTravelRateCount();

        /**
         * TODO: 汉化操作，将船舶信息汉化为中文，在页面显示
         */
        List<String> shipsCN = new ArrayList<>(ships.size());
        ships.forEach(l -> {
            shipsCN.add(ShipCN.getCNTrim(l));
        });

        Map<String, Object> resMap = new HashMap<>(4);
        resMap.put("dates", dates);
        /**
         * TODO: 由于堆叠图数据结构特殊，需要单独构造
         * TODO: 这里：将 x 轴数据统一行在一个列表中
         * TODO:      将每种船舶的数据按照不同的月分并且有序的放在一个列表中，js得到map对象后根据需要，提取对应数据直接显示
         */
        Map<String, Integer[]> shipMap = new HashMap<>(ships.size());
        ships.forEach(l -> {
            Integer[] arr = new Integer[dates.size()];
            int[] i = new int[]{0};
            dates.forEach(a -> {
                arr[i[0]] = 0;
                datas.forEach(b -> {
                    if (l.equals(b.get("ship_type")) && a.equals(b.get("sdatetime"))) {
                        arr[i[0]] = Integer.parseInt(b.get("count").toString());
                    }
                });
                i[0]++;
            });
            shipMap.put(ShipCN.getCNTrim(l), arr);
        });
        resMap.put("shipnames", ships);
        resMap.put("ships", shipMap);
        resMap.put("shipsCN", shipsCN);
        return resMap;
    }
}
